<template>
    <div class="dashboard-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <i class="el-icon-s-home home-icon"></i>
                <span class="system-title">课程管理系统</span>
            </div>

            <el-menu router :default-active="activeMenu" class="sidebar-menu" background-color="#2c3e50"
                text-color="#ecf0f1" active-text-color="#3498db">
                <el-menu-item :index="item.path" v-for="item in list" :key="item.path">
                    <i class="el-icon-user"></i>
                    <span>{{ item.meta.title }}</span>
                </el-menu-item>
            </el-menu>
        </div>

        <!-- 主内容区域 -->
        <div class="main-container">
            <!-- 顶部导航栏 -->
            <div class="top-navbar">
                <div class="navbar-right">
                    <el-button circle size="small" class="nav-button">
                        <i class="el-icon-full-screen"></i>
                    </el-button>

                    <el-dropdown @command="handleUserCommand">
                        <div class="user-info">
                            <el-avatar size="small" :src="userAvatar" />
                            <span class="username">admin</span>
                            <i class="el-icon-arrow-down dropdown-icon"></i>
                        </div>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                                <el-dropdown-item command="settings">设置</el-dropdown-item>
                                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>

            <!-- 主内容 -->
            <router-view></router-view>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            userAvatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
            list: []
        }
    },
    computed: {
        activeMenu() {
            // 提取嵌套路由的子路径部分
            const path = this.$route.path
            if (path.startsWith('/home/')) {
                return path.replace('/home/', '')
            }
            return path.substring(1)
        }
    },
    methods: {
        handleUserCommand(command) {
            switch (command) {
                case 'profile':
                    console.log('点击了个人信息')
                    break
                case 'settings':
                    console.log('点击了设置')
                    break
                case 'logout':
                    console.log('点击了退出登录');
                    this.$store.commit('user/updatainfo', {});
                    localStorage.removeItem('userInfo');
                    this.$router.push('/login').catch(() => {});
                    break
            }
        },
        getList() {
            // 通过路由信息渲染左侧菜单栏
            let list = this.$router.options.routes.find(item => item.path === '/home').children;

            // 获取用户权限信息
            console.log(this.$store.state.user.info.permission);
            let permission = this.$store.state.user.info.permission;

            // 通过用户权限，得到权限组
            
            console.log(this.$store.state.user.info.group);
            let group = this.$store.state.user.info.group;

            // 权限组+动态路由
            this.list = list.filter(item =>
                !item.meta.permission || group.indexOf(String(item.meta.permission)) !== -1
            )
        }
    },
    mounted() {
        this.getList()
        console.log(this.$router.options.routes.find(item => item.path === '/home').children);
    }
}
</script>

<style scoped>
.dashboard-container {
    display: flex;
    height: 100vh;
    background: #f5f5f5;
}

.sidebar {
    width: 250px;
    background: #2c3e50;
    color: white;
    overflow-y: auto;
}

.sidebar-header {
    display: flex;
    align-items: center;
    padding: 20px 16px;
    background: #34495e;
    border-bottom: 1px solid #1a252f;
}

.home-icon {
    font-size: 20px;
    margin-right: 10px;
    color: #3498db;
}

.system-title {
    font-size: 16px;
    font-weight: 600;
    color: white;
}

.sidebar-menu {
    border: none;
    background: #2c3e50;
}

.sidebar-menu .el-menu-item {
    height: 50px;
    line-height: 50px;
    padding-left: 20px !important;
    border-bottom: 1px solid #34495e;
}

.sidebar-menu .el-menu-item:hover {
    background-color: #34495e !important;
}

.sidebar-menu .el-menu-item.is-active {
    background-color: #3498db !important;
    color: white !important;
}

.main-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.top-navbar {
    height: 60px;
    background: white;
    border-bottom: 1px solid #e4e7ed;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 20px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.navbar-right {
    display: flex;
    align-items: center;
    gap: 15px;
}

.nav-button {
    color: #606266;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 5px 10px;
    border-radius: 4px;
    transition: background-color 0.3s;
}

.user-info:hover {
    background-color: #f5f7fa;
}

.username {
    font-size: 14px;
    color: #303133;
}

.dropdown-icon {
    font-size: 12px;
    color: #909399;
}
</style>